These functions come with Core PHP and do not require any installation for these functions to work. There is no need for runtime configuration directives to be defined in the php.ini file.
PHP Constants
There are some PHP supported constants that you can use with Directory functions. The two supported constants are-
- DIRECTORY_SEPERATOR
- PATH_SEPERATOR
PHP Directive functions
PHP supports below Directory functions-
1. chdir()
This function will allow you to change the current directory of the PHP to the directory that you will pass to this function.
Syntax
bool chdir ( string $directory )
Example
<?php echo getcwd() . "\n"; chdir('html'); echo getcwd() . "\n"; ?>
Output
/home/XXX /home/XXX/html
2. chroot()
This function will allow you to change the root directory of the current PHP to the directory that has been passed to the function. For this function to work you have to configure your PHP with enable-chroot-func and only root user is able to make this change.
Syntax
bool chroot ( string $directory )
Example
<?php chroot('/server/var/www'); ?>
3. dir()
This function will allow you to open a directory handle which returns an object. This object has three methods which you can use after opening a directory- read(), rewind() and close(). This function will take a parameter that will take the value of the directory to be passed.
Syntax
dirhandle bool dir ( string $directory )
Example
<?php $d = dir("/var/www/"); echo "Handle: " . $d->handle . "\n"; ?>
Output
Handle: Resource id #5
4. closedir()
This function will allow you to close the directory that has been passed as a parameter to the function. In order to close the directory, it must be opened beforehand.
Syntax
void closedir ( resource $dir_handle );
Example
<?php $dir_input = opendir("/var/XXX/images"); while (($file = readdir($dir_input)) !== false) { echo "file_name: " . $file . "<br />"; } closedir($dir_input); ?>
5. getcwd()
This function will provide you with the current working directory. This function will not take any parameter.
Syntax
string getcwd ( void );
Example
<?php echo getcwd() . "\n"; getcwd('html'); echo getcwd() . "\n"; ?>
6. opendir()
This function will allow you to open the passed directory to the function. This function can be used as a subsequent to the functions like- closedir(), rewinddir() and readdir() calls. This function will take two parameters among which the first parameter is mandatory and the second one is optional. The context will specify the set of operations that will modify the behaviour of a stream.
Syntax
resource opendir ( string $path [, resource $context] );
Example
<?php $dir_name = opendir("/var/XXX/images"); while (($file = readdir($dir_name)) !== false) { echo "file_name: " . $file . "<br />"; } closedir($dir_name); ?>
7. readdir()
This function will allow you to get the filename of the next file from the provided directory. All the file name will be displayed in the same order in which they are stored on the file system.
Syntax
string readdir ( resource $dir_handle );
Example
<?php $dir_name = opendir("/var/www/images"); while (($file = readdir($dir_name)) !== false) { echo "file_name: " . $file . "<br />"; } closedir($dir_name); ?>
8. rewinddir()
This function will reset the directory stream specified with the dir_handle to the start of the directory.
Syntax
void rewinddir ( resource $dir_handle );
Example
<?php $dir_name = opendir("/var/www/images"); while (($file =rewinddir($dir_name)) !== false) { echo "file_name: " . $file . "<br />"; } closedir($dir_name); ?>
9. scandir()
This function will provide you with the array of files and directory corresponding to the passed directory.
Syntax
array scandir ( string $directory [, int $sorting_order [, resource $context]] );
Example
<?php $dir_name = '/newfolder'; $files1 = scandir($dir_name); $files2 = scandir($dir_name, 1); print_r($files1); print_r($files2); ?>
People are also reading: